﻿using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace CompactZipStreams
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MemoryStream memoryStream = new MemoryStream();
            FillStream(memoryStream, 100);

            MemoryStream zippedMemoryStream = new MemoryStream();
            ZipOutputStream zipStream = new ZipOutputStream(zippedMemoryStream);
            ZipEntry zipEntry = new ZipEntry("myData");
            zipStream.PutNextEntry(zipEntry);
            FillStream(zipStream, 100);
            zipStream.CloseEntry();

            ZipInputStream zipInputStream = new ZipInputStream(zippedMemoryStream);
            zippedMemoryStream.Position = 0;
            MemoryStream fromZippedMemoryStream = new MemoryStream();
            ZipEntry entry = zipInputStream.GetNextEntry();
            byte[] outputBuffer = new byte[zipInputStream.Length];
            zipInputStream.Read(outputBuffer, 0, outputBuffer.Length);
            fromZippedMemoryStream.Write(outputBuffer, 0, outputBuffer.Length);

            MessageBox.Show(string.Format("Изначальная длинна: {0} bytes \n В архиве: {1} bytes \n Разархивированно в: {2} ", memoryStream.Length,zippedMemoryStream.Length,fromZippedMemoryStream.Length));


        }
        private static void FillStream(Stream stream, int times)
        {
            string temp = "The quick brown fox jumps over the lazy dog";
            string data = string.Empty;
            for (int i = 0; i < times; i++) data += temp;
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
            stream.Write(buffer, 0, buffer.Length);
        }
    }
}